package com.pfzy.eventbusdemo

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import org.greenrobot.eventbus.EventBus

/**
 * 总线框架 event bus
 */
class PublisherActivity : AppCompatActivity() {
    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_publisher)
    }

    /**
     * 在主线程中发布事件
     * @param view
     */
    fun onPublishEventOnMainThread(view: View?) {
        Log.d(TAG, "onPublishEventOnMainThread: ")
        val event = MyEvent("主线程的消息")
        EventBus.getDefault().post(event)
    }

    /**
     * 在子线程中发送事件
     * @param view
     */
    fun onPublishEventOnBGThread(view: View?) {
        Log.d(TAG, "onPublishEventOnBGThread: ")
        Thread {
            val event = MyEvent("后台线程的消息")
            EventBus.getDefault().post(event)
        }.start()
    }

    companion object {
        private const val TAG = "PublisherActivity"
    }
}